<?php

namespace app\admin\controller\thm;

use app\common\controller\Backend;

use app\admin\library\Auth;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

use think\Log;


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
//use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;


use fast\Http;

/**
 * 客戶手机记录
 *
 * @icon fa fa-circle-o
 */
class Message extends Backend
{
    
    /**
     * Message模型对象
     * @var \app\admin\model\thm\Message
     */
    protected $model = null;

    protected $noNeedRight = ['index2', 'sendmsg', 'csdx', 'jsdx'];


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\thm\Message;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("calltypeList", $this->model->getCalltypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    function convertUrlQuery($query)
    {
        $queryParts = explode('&', $query);
        $params = array();
        foreach ($queryParts as $param) {
            $item = explode('=', $param);
            $params[$item[0]] = $item[1];
        }
        return $params;
    }

    /**
     * 已收到短信
     */
    public function index()
    {
        //http://fastaws.test/admin/thm/message/index?mode=2&ids=9&ref=addtabs
        $url = $_SERVER['HTTP_REFERER'];
        $thmuserid = 0;
        if (false !== strpos($url, '/ids/')) {
            $urls = explode('?', $url);
            $url = $urls[0];
            $urls = explode('/', $url);
            $size = count($urls);
            $thmuserid = $urls[$size - 1];
            $thmuser = \app\admin\model\thm\User::get($thmuserid);
        } else {
            $url = $_SERVER['REQUEST_URI'];
            if (false !== strpos($url, '/ids/')) {
                $urls = explode('?', $url);
                $url = $urls[0];
                $urls = explode('/', $url);
                $size = count($urls);
                $thmuserid = $urls[$size - 1];
                $thmuser = \app\admin\model\thm\User::get($thmuserid);
            }
        }

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $where1 = "type = '0' ";
            $mobile = "";
            $username = "";
            if ($thmuser) {
                $name = $thmuser->name;
                $mobile = $thmuser->mobile;
                $username = $thmuser->username;
                $where1 .= "and mobile = '{$name}' ";//mobile变为imei
            }

            $total = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            foreach ($list as &$row) {
                $latlon = $row['latlon'];
                $latlon = str_replace(' ', '', $latlon);
                $row['mapurl'] = "http://maps.google.com.tw/maps?f=q&geocode=&q={$latlon}";
                $row['mobile'] = $mobile;
                $row['name'] = $username;
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        if ($thmuser) {
            $thmuser = $thmuser->toArray();
        }

        $this->view->assign("thmuser", $thmuser);
        return $this->view->fetch();
    }

    public function index2()
    {
        //http://fastaws.test/admin/thm/message/index?mode=2&ids=9&ref=addtabs
        $url = $_SERVER['HTTP_REFERER'];
        $thmuserid = 0;
        $smstype = 1;
        $thmuser = null;
        $smszfmobile = 0;
        if (false !== strpos($url, 'ids')) {
            $data = parse_url($url);
            if ($data && isset($data['query'])) {
                $query = $this->convertUrlQuery($data['query']);
                if ($query && isset($query['ids'])) {
                    $thmuserid = $query['ids'];
                    $thmuser = \app\admin\model\thm\User::get($thmuserid);
                }
                if ($query && isset($query['smstype'])) {
                    $smstype = $query['smstype'];
                }
                if ($query && isset($query['smszfmobile'])) {
                    $smszfmobile = intval($query['smszfmobile']);
                }
            }
        } else {
            $url = $_SERVER['REQUEST_URI'];
            if (false !== strpos($url, 'ids')) {
                $data = parse_url($url);
                if ($data && isset($data['query'])) {
                    $query = $this->convertUrlQuery($data['query']);
                    if ($query && isset($query['ids'])) {
                        $thmuserid = $query['ids'];
                        $thmuser = \app\admin\model\thm\User::get($thmuserid);
                    }
                    if ($query && isset($query['smstype'])) {
                        $smstype = $query['smstype'];
                    }
                    if ($query && isset($query['smszfmobile'])) {
                        $smszfmobile = intval($query['smszfmobile']);
                    }
                }
            }
        }

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $where1 = "type = '1' and smstype = {$smstype} ";

            $mobile = "";
            $username = "";
            if ($thmuser) {
                $name = $thmuser->name;
                $mobile = $thmuser->mobile;
                $username = $thmuser->username;
                $where1 .= "and mobile = '{$name}' ";//mobile变为imei
            }


            if ($smszfmobile > 0) {
                $where1 .= "and smszfmobile <> '' ";
            }

            $total = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            foreach ($list as &$row) {
                $row['mobile'] = $mobile;
                $row['name'] = $username;
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        if ($thmuser) {
            $thmuser = $thmuser->toArray();
        }

        $this->view->assign("thmuser", $thmuser);
        return $this->view->fetch();
    }

    public function sendmsg($ids) {
        $thmuser = \app\admin\model\thm\User::get($ids);
        if (!$thmuser) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isPost())  {
            $zfmobile = $_POST['zfmobile'];
            $zfmobile = str_replace(array("\n","\r"," "), "", $zfmobile);
//            $list = explode(";", $zfmobile);
//
//            foreach ($list as $row) {
//                if (empty($row)) {
//                    continue;
//                }
//                $msglog = new \app\admin\model\thm\Msglog();
//                $msglog->thm_user_id = $_POST['thm_user_id'];
//                $msglog->msg = $_POST['msg'];
//                $msglog->zfmobile = $row;
//                $msglog->save();
//            }
            $msglog = new \app\admin\model\thm\Msglog();
            $msglog->thm_user_id = $_POST['thm_user_id'];
            $msglog->msg = $_POST['msg'];
            $msglog->zfmobile = $zfmobile;
            $msglog->save();

            $this->success("發送短信 OK!", null, ['id' => $msglog->thm_user_id]);

        }

        $this->view->assign("thm_user_id", $ids);
        return $this->view->fetch();
    }

    public function csdx()
    {
        $smszfmobile = 1;
        $smstype = 1;

        $url = $_SERVER['HTTP_REFERER'];
        $thmuserid = 0;
        if (false !== strpos($url, '/ids/')) {
            $urls = explode('?', $url);
            $url = $urls[0];
            $urls = explode('/', $url);
            $size = count($urls);
            $thmuserid = $urls[$size - 1];
            $thmuser = \app\admin\model\thm\User::get($thmuserid);
        } else {
            $url = $_SERVER['REQUEST_URI'];
            if (false !== strpos($url, '/ids/')) {
                $urls = explode('?', $url);
                $url = $urls[0];
                $urls = explode('/', $url);
                $size = count($urls);
                $thmuserid = $urls[$size - 1];
                $thmuser = \app\admin\model\thm\User::get($thmuserid);
            }
        }

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $where1 = "type = '1' and smstype = {$smstype} ";

            $mobile = "";
            $username = "";
            if ($thmuser) {
                $name = $thmuser->name;
                $mobile = $thmuser->mobile;
                $username = $thmuser->username;
                $where1 .= "and mobile = '{$name}' ";//mobile变为imei
            }


            if ($smszfmobile > 0) {
                $where1 .= "and smszfmobile <> '' ";
            }

            $total = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            foreach ($list as &$row) {
                $row['mobile'] = $mobile;
                $row['name'] = $username;
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

    }

    public function jsdx()
    {
        $smszfmobile = 0;
        $smstype = 1;

        $url = $_SERVER['HTTP_REFERER'];
        $thmuserid = 0;
        if (false !== strpos($url, '/ids/')) {
            $urls = explode('?', $url);
            $url = $urls[0];
            $urls = explode('/', $url);
            $size = count($urls);
            $thmuserid = $urls[$size - 1];
            $thmuser = \app\admin\model\thm\User::get($thmuserid);
        } else {
            $url = $_SERVER['REQUEST_URI'];
            if (false !== strpos($url, '/ids/')) {
                $urls = explode('?', $url);
                $url = $urls[0];
                $urls = explode('/', $url);
                $size = count($urls);
                $thmuserid = $urls[$size - 1];
                $thmuser = \app\admin\model\thm\User::get($thmuserid);
            }
        }

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $where1 = "type = '1' and smstype = {$smstype} and smszfmobile = '' ";

            $mobile = "";
            $username = "";
            if ($thmuser) {
                $name = $thmuser->name;
                $mobile = $thmuser->mobile;
                $username = $thmuser->username;
                $where1 .= "and mobile = '{$name}' ";//mobile变为imei
            }

            $total = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            foreach ($list as &$row) {
                $row['mobile'] = $mobile;
                $row['name'] = $username;
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

    }

    public function fsdx()
    {
        $smszfmobile = 0;
        $smstype = 2;

        $url = $_SERVER['HTTP_REFERER'];
        $thmuserid = 0;
        if (false !== strpos($url, '/ids/')) {
            $urls = explode('?', $url);
            $url = $urls[0];
            $urls = explode('/', $url);
            $size = count($urls);
            $thmuserid = $urls[$size - 1];
            $thmuser = \app\admin\model\thm\User::get($thmuserid);
        } else {
            $url = $_SERVER['REQUEST_URI'];
            if (false !== strpos($url, '/ids/')) {
                $urls = explode('?', $url);
                $url = $urls[0];
                $urls = explode('/', $url);
                $size = count($urls);
                $thmuserid = $urls[$size - 1];
                $thmuser = \app\admin\model\thm\User::get($thmuserid);
            }
        }

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $where1 = "type = '1' and smstype = {$smstype} ";

            $mobile = "";
            $username = "";
            if ($thmuser) {
                $name = $thmuser->name;
                $mobile = $thmuser->mobile;
                $username = $thmuser->username;
                $where1 .= "and mobile = '{$name}' ";//mobile变为imei
            }


            if ($smszfmobile > 0) {
                $where1 .= "and smszfmobile <> '' ";
            }

            $total = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($where1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            foreach ($list as &$row) {
                $row['mobile'] = $mobile;
                $row['name'] = $username;
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

    }

}
